﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using KeepAnimals.DAL;

namespace KeepAnimals.BLL
{
    public class Box
    {
        //Déclaration des constantes pour savoir si une box est vide ou pleine. (0 si vide , 1 si plein)
        public const int BOX_EMPTY = 0;
        public const int BOX_FULL = 1;


        public int Id { get; set; }
        public int IdAnimal { get; set; }
        public int IdDepartment { get; set; }    
        public int Empty { get; set; }

        //private Connection con;
        public Box()
        {
           // con = Connection.getInstance() ;

        }
        //Fonction pour ajouter un box dans la base de donnée depuis le formulaire de la vue. 
        public int AddBoxInDatabase()
        {

            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "INSERT INTO box(IdAnimal,IdDepartment,Empty)VALUES(?IdAnimal,?IdDepartment,?Empty);";
            command.Parameters.AddWithValue("?IdAnimal", this.IdAnimal);
            command.Parameters.AddWithValue("?IdDepartment", this.IdDepartment);
            command.Parameters.AddWithValue("?Empty", this.Empty);

            Connection.AddUpdateCommandInDatabase(command);
            return Connection.ReturnLastId(Connection.TABLE_NAME_BOX);
        }
        
        /// <summary>
        /// Updates the animal in database.
        /// </summary>
        public void UpdateBoxInDatabase()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();

            command.CommandText = "UPDATE box SET Id = ?Id,IdAnimal = ?IdAnimal,IdDepartment = ?IdDepartment,Empty =?Empty WHERE id =" + this.Id + ";";
            command.Parameters.AddWithValue("?Id", this.Id);
            command.Parameters.AddWithValue("?IdAnimal", this.IdAnimal);
            command.Parameters.AddWithValue("?IdDepartment", this.IdDepartment);
            command.Parameters.AddWithValue("?Empty", this.Empty);
          
            Connection.AddUpdateCommandInDatabase(command);
        }
        /// <summary>
        /// Gets the animal by identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static Box GetBoxById(int id)
        {
            
            Connection.OpenConnection();
            Box tempBox = new Box();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM box WHERE Id = "+id+";";
            MySqlDataReader rdr = Connection.getAllData(command);
            try
            {
                while (rdr.Read())
                {
                    if (!rdr.IsDBNull(0))
                        tempBox.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempBox.IdAnimal = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempBox.IdDepartment = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempBox.Empty = rdr.GetInt32(3);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return tempBox;
        }

        /// <summary>
        /// Gets the box by identifier department.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public static List<Box> GetBoxsByIdDepartment(int idDepartment)
        {
            List<Box> boxs = new List<Box>();
            Connection.OpenConnection();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM box WHERE IdDepartment = " + idDepartment + ";";
            MySqlDataReader rdr = Connection.getAllData(command);
            try
            {
                while (rdr.Read())
                {
                    Box tempBox = new Box();

                    if (!rdr.IsDBNull(0))
                        tempBox.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempBox.IdAnimal = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempBox.IdDepartment = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempBox.Empty = rdr.GetInt32(3);
                    boxs.Add(tempBox);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }

            return boxs;
        }

        //Fonction pour envoyer a la vu la liste de l'animal selection , exemple chat / chien , on passe un id de la categorie animal
        public static  List<Box> GetListOfAllBox()
        {
            Connection.OpenConnection();
            List<Box> boxs = new List<Box>();
            MySqlCommand command = Connection.getInstanceOfCommand();
            command.CommandText = "SELECT * FROM box";
            MySqlDataReader rdr = Connection.getAllData(command);

             try
            {
                while (rdr.Read())
                {
                    Box tempBox = new Box();
                    if (!rdr.IsDBNull(0))
                        tempBox.Id = rdr.GetInt32(0);
                    if (!rdr.IsDBNull(1))
                        tempBox.IdAnimal = rdr.GetInt32(1);
                    if (!rdr.IsDBNull(2))
                        tempBox.IdDepartment = rdr.GetInt32(2);
                    if (!rdr.IsDBNull(3))
                        tempBox.Empty = rdr.GetInt32(2);
                    boxs.Add(tempBox);
                }
            }
            finally
            {
                rdr.Close();
                Connection.CloseConnection();
            }
            return boxs;
        }

        public void UpdateIDAnimalInDatabaseWithIdBox()
        {
            MySqlCommand command = Connection.getInstanceOfCommand();
            //MySqlCommand command = Connection.connection.CreateCommand();

            command.CommandText = "UPDATE box SET IdAnimal = ?IdAnimal, Empty = ?Empty WHERE id =" + this.Id + ";";
            command.Parameters.AddWithValue("?IdAnimal", this.IdAnimal);
            command.Parameters.AddWithValue("?Empty", this.Empty);
            
            Connection.AddUpdateCommandInDatabase(command);
            Console.WriteLine(command.CommandText);
        }
    }
}
